# -*-encoding=utf-8-*-

import pandas as pd
import matplotlib.pyplot as plt


df_AQI  = pd.read_csv("AQI.csv",   encoding="utf-8")
# df_PM25 = pd.read_csv("PM2.5.csv", encoding="utf-8")
# df_PM10 = pd.read_csv("PM10.csv",  encoding="utf-8")

'''仅显示AQI最高值，最低值和平均值'''
# time = df_AQI.columns.tolist()[1:]
# AQI_max = [max(df_AQI[t]) for t in time]
# AQI_min = [min(df_AQI[t]) for t in time]
# AQI_ave = [
#     sum([i for i in df_AQI[t].replace(float("nan"), 0) if i != 0])
#         /len([i for i in df_AQI[t].replace(float("nan"), 0) if i != 0])
#         for t in time
# ]
# time = [t.replace("2022.", "") for t in time]

# plt.title("空气质量分析")
# plt.rcParams["font.sans-serif"] = ["simHei"]
# plt.xlabel("时间")
# plt.ylabel("AQI指数")
# plt.xticks(rotation=270)
# plt.plot(
#     time, AQI_max, color="r", linestyle="-", label="最高AQI", marker="o"
# )
# plt.plot(
#     time, AQI_ave, color="b", linestyle="-", label="平均AQI", marker="o"
# )
# plt.plot(
#     time, AQI_min, color="g", linestyle="-", label="最低AQI", marker="o"
# )
# plt.plot(
#     time, [50 for _ in range(len(time))], color="black", linestyle="-.",
#     label="优"
# )
# plt.plot(
#     time, [100 for _ in range(len(time))], color="black", linestyle="--",
#     label="良"
# )

# plt.legend()
# plt.show()


'''天数统计'''
time = df_AQI.columns.tolist()[1:]
AQI_max = [max(df_AQI[t]) for t in time]
cnt = [0, 0, 0, 0]  # 优，良，轻度污染，重度污染
for AQI in AQI_max:
    if AQI <= 50:
        cnt[0] += 1
    elif AQI <= 100:
        cnt[1] += 1
    elif AQI <= 150:
        cnt[2] += 1
    elif AQI <= 200:
        cnt[3] += 1

for level, n in zip(["优", "良", "轻度污染", "中度污染"], cnt):
    print(f"{level}：{n}天")



'''各时刻AQI平均值变化趋势'''
time = [i for i in range(1, 25)]
AQI_ave_per_tick = [
    sum(df_AQI.loc[t][1:].dropna())/len(df_AQI.loc[t][1:].dropna())
        for t in range(0, 24)
]

plt.title("空气质量分析")
plt.rcParams["font.sans-serif"] = ["simHei"]
plt.xlabel("时间")
plt.ylabel("AQI指数")
plt.xticks(rotation=270)

plt.plot(
    [f"{t}:00" for t in range(1, 25)], AQI_ave_per_tick, color="blue",
    linestyle="-", label="平均AQI", marker="o"
)
plt.plot(
    time, [50 for _ in range(len(time))], color="black", linestyle="-.",
    label="优"
)

plt.legend()
plt.show()